package com.miyake.dsp.file;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.miyake.dsp.DspComposite;
import com.miyake.dsp.ProcessorFactory;
import com.miyake.dsp.filter.FilterSetting;
import com.thoughtworks.xstream.XStream;

public class CompositePersistentImplXml implements CompositePersistent {
	class Stored {
		
		public Stored(ProcessorFactory factory) {
			super();
			this.factory = factory;
			list = new ArrayList<StoredElement>();
		}
		private ProcessorFactory factory;
		private List<StoredElement> list;
		public ProcessorFactory getFactory() {
			return factory;
		}
		public void setFactory(ProcessorFactory factory) {
			this.factory = factory;
		}
		public List<StoredElement> getList() {
			return list;
		}
		public void setList(List<StoredElement> list) {
			this.list = list;
		}
	};
	class StoredElement {
		public StoredElement(Object setting, boolean bypass) {
			super();
			this.setting = setting;
			this.bypass = bypass;
		}
		public Object setting;
		public boolean bypass;
	};
	
	@Override
	public void save(String name, DspComposite composite,
			ProcessorFactory factory) {
		
		XStream xstream = new XStream();
    	xstream.omitField(FilterSetting.class, "handlers");
    	Stored stored = new Stored(factory);
    	DspComposite current = composite;

    	while(true) {
    		stored.list.add(new StoredElement(current.getSetting(), current.isBypass()));
    		if (current.getList().size() > 0) {
    			current = current.getList().get(0);
    		}
    		else {
    			break;
    		}
    	}
    	
    	String xml = xstream.toXML(stored);
    	try {
			BufferedWriter writeFile = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(name)));
			writeFile.write(xml);
			writeFile.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
	    }
	}

	@Override
	public DspComposite load(String name) {
		XStream xstream = new XStream();
		BufferedReader reader = null;
		String tmp = null;
		String xml2 = "";
		
		DspComposite entryPoint = null;
		
		try {
			reader = new BufferedReader(new InputStreamReader(new FileInputStream(name)));
			
			while((tmp = reader.readLine()) != null) {
				xml2 += tmp;
			}
			Stored stored = (Stored)xstream.fromXML(xml2);
			
			
			Iterator<StoredElement> it = stored.getList().iterator();
			ProcessorFactory factory = stored.getFactory();
			while(it.hasNext()) {
				it.next();
			}
			
			reader.close();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();		
		} catch (IOException e) {
			e.printStackTrace();
		}
		return entryPoint; 
	}

}
